package com.boot.redisson;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Import;

@Slf4j
@ConditionalOnClass(Redisson.class)
@Import(DistributedLockHandler.class)
public class RedissonAutoConfiguration {

    //    public static final String REDIS_PROTOCOL = "redis://%s:%d";
    //
    //    public RedisConnectionConfiguration connectionConfiguration;
    //
    //    public RedissonAutoConfiguration(RedisConnectionConfiguration connectionConfiguration) {
    //        this.connectionConfiguration = connectionConfiguration;
    //    }
    //
    //    @Bean
    //    public Config redissonConfig() {
    //        if (connectionConfiguration.getSentinelConfig() != null) {
    //            return createSentinelConfig();
    //        }
    //        if (connectionConfiguration.getClusterConfiguration() != null) {
    //            return createClusterConfig();
    //        }
    //        return createStandaloneConfig();
    //    }
    //
    //    private Config createStandaloneConfig() {
    //        RedisStandaloneConfiguration standaloneConfig = connectionConfiguration.getStandaloneConfig();
    //        Config config = new Config();
    //        String address = String.format(REDIS_PROTOCOL, standaloneConfig.getHostName(), standaloneConfig.getPort());
    //        config.useSingleServer().setAddress(address);
    //        config.useSingleServer().setDatabase(standaloneConfig.getDatabaseOrElse(() -> 0));
    //        RedisPassword password = standaloneConfig.getPassword();
    //        if (password.isPresent()) {
    //            config.useSingleServer().setPassword(String.valueOf(password.get()));
    //        }
    //        log.info("Redisson 单机模式初始化，地址: {}", address);
    //        return config;
    //    }
    //
    //    private Config createSentinelConfig() {
    //        RedisSentinelConfiguration sentinelConfig = connectionConfiguration.getSentinelConfig();
    //        Config config = new Config();
    //        config.useSentinelServers().setMasterName(sentinelConfig.getMaster().getName());
    //        Set<RedisNode> redisNodes = sentinelConfig.getSentinels();
    //        List<String> addresses = redisNodes.stream()
    //                .map(node -> String.format(REDIS_PROTOCOL, node.getHost(), node.getPort()))
    //                .collect(Collectors.toList());
    //        config.useSentinelServers().setSentinelAddresses(addresses);
    //        config.useSentinelServers().setDatabase(sentinelConfig.getDatabaseOrElse(() -> 0));
    //
    //        RedisPassword sentinelPassword = sentinelConfig.getSentinelPassword();
    //        if (sentinelPassword.isPresent()) {
    //            config.useSentinelServers().setSentinelPassword(String.valueOf(sentinelPassword.get()));
    //        }
    //        RedisPassword dataNodePassword = sentinelConfig.getDataNodePassword();
    //        if (dataNodePassword.isPresent()) {
    //            config.useSentinelServers().setPassword(String.valueOf(dataNodePassword.get()));
    //        }
    //
    //        log.info("Redisson 哨兵模式初始化，地址: {}", addresses);
    //        return config;
    //    }
    //
    //    private Config createClusterConfig() {
    //        RedisClusterConfiguration clusterConfiguration = connectionConfiguration.getClusterConfiguration();
    //        Config config = new Config();
    //        Set<RedisNode> redisNodes = clusterConfiguration.getClusterNodes();
    //        List<String> addresses = redisNodes.stream()
    //                .map(node -> String.format(REDIS_PROTOCOL, node.getHost(), node.getPort()))
    //                .collect(Collectors.toList());
    //        config.useClusterServers().setNodeAddresses(addresses);
    //
    //        RedisPassword password = clusterConfiguration.getPassword();
    //        if (password.isPresent()) {
    //            config.useSentinelServers().setPassword(String.valueOf(password.get()));
    //        }
    //        log.info("Redisson 集群模式初始化，地址: {}", addresses);
    //        return config;
    //    }
    //
    //    @Bean(destroyMethod = "shutdown")
    //    public RedissonClient redissonClient(Config config, ObjectProvider<Codec> codecObjectProvider) {
    //        config.setCodec(codecObjectProvider.orderedStream().findFirst().orElse(JsonJacksonCodec.INSTANCE));
    //        try {
    //            RedissonClient redissonClient = Redisson.create(config);
    //            log.info("Redisson 连接成功!");
    //            return redissonClient;
    //        } catch (RuntimeException e) {
    //            log.error("Redisson 初始化失败!");
    //            throw e;
    //        }
    //    }
    //
    //    @Bean
    //    @ConditionalOnMissingBean
    //    public RedissonLock redissonLock(RedissonClient redissonClient) {
    //        return new RedissonLock(redissonClient);
    //    }

}

